//
//  SYYueJuanStudentsView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYYueJuanStudentsView.h"
#import "SYYJStudentsTableViewCell.h"

@interface SYYueJuanStudentsView () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *curTableView;// 表
@property (strong, nonatomic) UIView *fgxView;
@property (strong, nonatomic) UIView *leftFgxView;//左侧分割
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (assign, nonatomic) NSInteger selectIndex;
@property (assign, nonatomic) BOOL isDetails;//是否是试题详情页面


@end


@implementation SYYueJuanStudentsView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}
- (void)initSubView {
    //表
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
    [self addSubview:self.fgxView];
    [self.fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.width.mas_equalTo(1);
    }];
    
    [self addSubview:self.leftFgxView];
    [self.leftFgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self);
        make.width.mas_equalTo(1);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYYJStudentsTableViewCell *studentsCell = [tableView dequeueReusableCellWithIdentifier:@"SYYJStudentsTableViewCell" forIndexPath:indexPath];
    studentsCell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.isDetails == YES) {
        studentsCell.studentModel = self.dataListArr[indexPath.row];
        studentsCell.bgView.backgroundColor = [UIColor whiteColor];
    } else {
        studentsCell.yjstModel = self.dataListArr[indexPath.row];
        if (self.selectIndex == indexPath.row) {
            studentsCell.bgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        } else {
            studentsCell.bgView.backgroundColor = [UIColor whiteColor];
        }
    }
    
    
    return studentsCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isDetails == YES) {
        return;//试卷详情页面进来。不执行下边的方法
    }
    self.selectIndex = indexPath.row;
    if ([self.delegate respondsToSelector:@selector(selectCellModel:selectIndex:)]) {
        [self.delegate selectCellModel:self.dataListArr[indexPath.row] selectIndex:indexPath.row];
    }
    [self.curTableView reloadData];
}

- (void)requestDataArr:(NSMutableArray *)dataListArr selectIndex:(NSInteger)index {
    self.dataListArr = dataListArr;
    self.isDetails = NO;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    self.selectIndex = index;
    [self.curTableView reloadData];
    
    [self tableView:self.curTableView didSelectRowAtIndexPath:indexPath];
}
//试卷详情
- (void)requestDataShiTiDetailsArr:(NSMutableArray *)detailsArr select:(BOOL)isSelect {
    self.dataListArr = detailsArr;
    self.isDetails = isSelect;
    [self.curTableView reloadData];
}

- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
        [_curTableView registerCellWithNIbNames:@[@"SYYJStudentsTableViewCell"]];
    }
    return _curTableView;
}

- (UIView *)fgxView {
    if (!_fgxView) {
        _fgxView = [[UIView alloc] init];
        _fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    }
    return _fgxView;
}
- (UIView *)leftFgxView {
    if (!_leftFgxView) {
        _leftFgxView = [[UIView alloc] init];
        _leftFgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    }
    return _leftFgxView;
}

@end
